﻿<script>
    common.setMainTitle('Deal Categories');
</script>
<form id="mainform" name='frm' method=post action="">
   	<div>
		<div id="toolbar-button">
			<div class="item" onclick="add()" name="addButton" id="addButton" title="Add new Parent Category">
				<div id="addnew-global-button"></div>
			</div>
			<div class="item" onclick="deleteAll()" name="addButton" id="deleteButton" title="Delete">
				<div id="delete-global-button"></div>
			</div>
			<div class="item" onclick="changeStatusMultiItems(1)" name="activateButton" id="activateButton" title="Activate">
				<div id="save-global-button"></div>
			</div>
			<div class="item" onclick="changeStatusMultiItems(0)" name="deactivateButton" id="deactivateButton" title="Deactivate">
				<div id="back-global-button"></div>				
			</div>
		</div>
	</div>
    <table style="margin-top: 10px;clear: both" border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
        <thead>
            <tr>
                <th width="3%" class="table-header-repeat line-left" style="text-align: center" nowrap >
                    <input type=checkbox name=DELALL onclick="SelectAll(this,'ITEM')">
                </th>
                <th class="table-header-repeat line-left" width="80%" style="text-align: left"><a>NAME</a></th>
				<th class="table-header-repeat line-left" width="15%" style="text-align: center"><a>SUBCATEGORIES</a></th>
				<th class="table-header-repeat line-left" width="5%" style="text-align: center"><a>STATUS</a></th>
				<th class="table-header-repeat line-left" width="" style="text-align: center"><a>ACTION</a></th>
            </tr>
        </thead>
        <tfoot>
        </tfoot>
        <tbody>
            <?php
			if (count($this->data) == 0) {
				echo "<tr><td colspan=6>There is not any data</td></tr>";
			} else {
				$stt = 1;
			}
			foreach ($this->data as $item) {
			?>
            <tr id="<? echo 'row'.$item['DEALCATEGORYID']?>" >
                <td style="text-align: center;padding-left: 0">
                    <input id="<?=$item['DEALCATEGORYID'] ?>" type="checkbox" name="ITEM[]" value="<?=$item['DEALCATEGORYID'] ?>" />
                </td>
                <td> 
					<div>
					<? if($item['SUBCOUNT']!=0){ ?>
                        <div class="expand-icon"></div>
						<a style="font-size: 14px;" class="expandable-table-category-name" href="javascript:expand(<?=$item['DEALCATEGORYID'] ?>,1)"><b><?=$item['NAME']; ?></b></a>
					<? } else { ?>
						<div class="expand-icon-blank"></div>
						<div style="font-size: 14px;" class="unexpandable-table-category-name"><b><?=$item['NAME']; ?></b></div>
					<? } ?>
					</div>
                </td>
				<td style="text-align: center;padding-left: 0"> 
					<div>
                        <?=$item['SUBCOUNT']; ?>
					</div>
                </td>
                <td style="text-align: center;padding-left: 0">
					<span id="status<?=$item['DEALCATEGORYID']?>">
                        <? if($item['STATUS']!='1') { ?>
                            <a href="#" onClick="changeStatus(<?=$item['DEALCATEGORYID'] ?>,1)">Inactive</a>
                        <?} else {?>
                            <a href="#" onClick="changeStatus(<?=$item['DEALCATEGORYID'] ?>,0)">Active</a>
                        <?}?>
                    </span>
                </td>
				<td style="text-align: center;padding-left: 0"> 
					<div>
					<select id="<?php echo 'actionbox'.$item['DEALCATEGORYID'] ?>" name="<?php echo 'actionbox'.$item['DEALCATEGORYID'] ?>" onchange="chooseAction(<?=$item['DEALCATEGORYID'] ?>,this)">
						<option value="0" >Choose an Action</option>
						<option value="<?=$item['DEALCATEGORYID'] ?>" >Edit</option>
						<option value="<?=$item['DEALCATEGORYID'] ?>" >Delete</option>
						<option value="<?=$item['DEALCATEGORYID'] ?>" >Add Sub Category</option>
					</select>
					</div>
                </td>
            </tr>
			<? if($item['SUBCOUNT']!=0){ ?>
			<tr id="<? echo 'row-ajax'.$item['DEALCATEGORYID']?>" style="display: none;text-align: center" class="row-ajax-loading">
				<td colspan="5" style="text-align: center;border: 0px">
					<div class="loading-ajax-img"></div>
				</td>
			</tr>
			<? } ?>
			<? } ?>
        </tbody>
    </table>

</form>
<script type="text/javascript">
	
	$(document).ready(function(){
		/* $('row-ajax' + itemID).hide(); */
	});
	
    function chooseAction(itemID,object){
		text = $("#actionbox"+itemID+ " option:selected").text();
		if(text == 'Edit'){
			window.location = "/admin/dealcategories/input/id/"+itemID;
		}else if(text == "Delete"){
			check = confirm("Do you want to delete this user?");
			if(check){
				deleteItem(itemID);
			}
		}else if(text == "Add Sub Category"){
			window.location = "/admin/dealcategories/input/parentid/" +itemID;
		}
    }
    
    function add(){
        window.location ='/admin/dealcategories/input';
    }
	
    function SelectAll(selobj,childobj){
         var arr = document.getElementsByName(childobj+"[]");
         for(var i=0;i<arr.length;i++){
            arr[i].checked = selobj.checked;
         }
    }
	
	function deleteItem(itemID){
		$.ajax({
			url: "/admin/dealcategories/delete",
			type: "POST",
			data: {itemID : itemID},
			dataType: "text",
			success: function(data) {
				var items = data.split(',');
				for(i=0;i<items.length;i++){
					$('#row' + $.trim(items[i])).remove();
				}
			}
		});
	}
	
    function deleteAll()
    {
    	  var ln = 0;
          var arr = document.getElementsByName('ITEM[]');
          var arrValue = new Array();
			var j=0;
				
			for(var i = 0 ; i < arr.length ;i++ )
			{
					if(arr[i].checked == true)
					{
							arrValue[j] = arr[i].value;
							j++;
					}
			}
		  for(var i = 0 ; i < arr.length ;i++ )
          {
			  if(arr[i].checked == true)
			  {
					  ln = 1;
					  break;
			  }
          }
          
          if(ln == 1)
          {
			  if(confirm("<?php echo "Do you want to delete these categories?"; ?>")){	
					$.ajax({
						url: "/admin/dealcategories/deletemultiitem",
						type: "POST",
						data: {itemIDs : arrValue},
						dataType: "text",
						success: function(data) {
							var items = data.split(',');
							for(i=0;i<items.length;i++){
								$('#row' + $.trim(items[i])).remove();
							}
						}
					});
			  }
          }
          else
          {
                  alert("You must choose at least 1 item!");
          }
    }
	
	function expand(itemId, level){
		$.ajax({
			url: "/admin/dealcategories/expand",
			type: "POST",
			data: {itemID : itemId, level: level},
			dataType: "text",
			success: function(data) {
				$('#row-ajax' + itemId).hide();
				$('#row' + itemId).after(data);
				$('.child-row-' + itemId).fadeIn('fast');
				$('#row' + itemId + ' td div .expand-icon').attr('class', 'collapse-icon');
				$('#row' + itemId + ' td div a').attr('href', 'javascript:collapse('+ itemId +')');
			},
			beforeSend: function(){
				$('#row-ajax' + itemId).fadeIn('fast');
			}
		});
		
	}
	
	function collapse(itemID){
		$('.child-row-' + itemID).fadeOut('fast');
		$('#row' + itemID + ' td div .collapse-icon').attr('class', 'expand-icon');
		$('#row' + itemID + ' td div a').attr('href', 'javascript:showSubCategories('+ itemID +')');
	}
	
	function showSubCategories(itemID){
		$('.child-row-' + itemID).fadeIn('fast');
		$('#row' + itemID + ' td div .expand-icon').attr('class', 'collapse-icon');
		$('#row' + itemID + ' td div a').attr('href', 'javascript:collapse('+ itemID +')');
		
	}
	
	//Gui thong tin status len server
    function changeStatus(itemID, status){
        $.ajax({
			url: "/admin/dealcategories/changestatus",
			type: "POST",
			data: {itemID : itemID, status: status},
			dataType: "text",
			success: function(data) {
				var items = data.split(',');
				for(i=0;i<items.length;i++){
					if(status == 0){
						$('#status' + $.trim(items[i])).html('<a href="#" onClick="changeStatus('+ items[i]+ ',1)">Inactive</a>');
					} else {
						$('#status' + $.trim(items[i])).html('<a href="#" onClick="changeStatus('+ items[i]+ ',0)">Active</a>');
					}
				}
			}
		});
    }
	
	function changeStatusMultiItems(status){
		var ln = 0;
		var arr = document.getElementsByName('ITEM[]');
		var arrValue = new Array();
		var j=0;
			
		for(var i = 0 ; i < arr.length ;i++ )
		{
				if(arr[i].checked == true)
				{
						arrValue[j] = arr[i].value;
						j++;
				}
		}
		for(var i = 0 ; i < arr.length ;i++ )
		{

				if(arr[i].checked == true)
				{
						ln = 1;
						break;
				}
		}
		if(ln == 1)
		{
			var question;
			if(status ==1){
				question = 'Do you want to activate these items?';
			} else {
				question = 'Do you want to inactivate these items?';
			}
			
			if(confirm(question)){
				
					$.ajax({
						url: "/admin/dealcategories/changestatusmultiitems",
						type: "POST",
						data: {itemIDs : arrValue, status: status},
						dataType: "text",
						success: function(data) {
							var items = data.split(',');
							for(i=1;i<items.length;i++){
								if(status == 0){
									$('#status' + $.trim(items[i])).html('<a href="#" onClick="changeStatus('+ items[i]+ ',1)">Inactive</a>');
								} else {
									$('#status' + $.trim(items[i])).html('<a href="#" onClick="changeStatus('+ items[i]+ ',0)">Active</a>');
								}
							}
						}
					});
			}
		}
		else
		{
				alert("You must choose at least 1 item!");
		}
	}

</script>